//
//  ViewController.m
//  添加各种手势识别UIGestureRecognizer
//
//  Created by hyl on 16/6/20.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTap];
    [self setPan];
    [self setSwipe];
    [self setLongPress];
    [self setRotation];
    [self setPinch];
}

#pragma mark - tap敲击
/**
 *  1.numberOfTapsRequired设置点击几次响应
 *  2.代理与(BOOL) gestureRecognizer结合，可确定图片哪个区域可以点，哪些不能点
 */
-(void) setTap{
    //创建点按手势
    //initWithTarget:谁监听这个事件呢，一般控制器
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    //delegate
    tap.delegate=self;
    //click counts
    tap.numberOfTapsRequired=2;
    //添加手势，用的是多态addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer（父类）
    [self.imageView addGestureRecognizer:tap];
}

-(void) tap:(UITapGestureRecognizer *) tap{
    NSLog(@"%s",__func__);
}

#pragma mark - pan拖拽
/**
 *  复位是指返回到起始的状态，再能过相对上一个状态tansform来解决一些问题
 */
-(void) setPan{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    
    [self.imageView addGestureRecognizer:pan];
}

-(void) pan:(UIPanGestureRecognizer *) pan{
    NSLog(@"%s",__func__);
//    CGPoint curP=[pan locationInView:self.imageView];
    //获取图片移动距离
    CGPoint curP=[pan translationInView:self.imageView];
    self.imageView.transform=CGAffineTransformTranslate(self.imageView.transform, curP.x, curP.y);
    //复位
    [pan setTranslation:CGPointZero inView:self.imageView];
}
#pragma mark - swipe轻扫
-(void) setSwipe{
    //添加向左滑动
    UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipe.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:swipe];
    //添加向右滑动
    UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipeDown.direction=UISwipeGestureRecognizerDirectionRight;
    [self.imageView addGestureRecognizer:swipeDown];
    
}
-(void) swipe:(UISwipeGestureRecognizer *)swipe{
    NSLog(@"%s",__func__);
}
#pragma mark - longPress长按
-(void) setLongPress{
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    [self.imageView addGestureRecognizer:longPress];
}
/**
 *  长按的一些注意点，那就是长按时触发一次，离开时再触发一次
 *  加个判断，结束与开始选一个触发的时间
 *  @param longPress 传参
 */
-(void) longPress:(UILongPressGestureRecognizer *) longPress{
    if (longPress.state==UIGestureRecognizerStateBegan) {
        NSLog(@"%s",__func__);
    }
    
}
#pragma mark - rotation旋转
-(void) setRotation{
    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    rotation.delegate=self;
    [self.imageView addGestureRecognizer:rotation];
}
-(void) rotation:(UIRotationGestureRecognizer *) rotation{
    NSLog(@"%s",__func__);
    NSLog(@"%f",rotation.rotation);
    self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
    [rotation setRotation:0];
//    rotation.rotation=0;
    
}
#pragma mark - pinch捏合，用于缩放
-(void) setPinch{
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    pinch.delegate=self;
    [self.imageView addGestureRecognizer:pinch];
}
-(void) pinch:(UIPinchGestureRecognizer *) pinch{
    NSLog(@"%s",__func__);
    self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    pinch.scale=1;
}

#pragma mark - UIGestureRecognizerDelegate
//与tap结合
//-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//    //获取当前触摸点在self.imageView的哪个位置
//    CGPoint curP=[touch locationInView:self.imageView];
//    if (curP.x<self.imageView.bounds.size.width*0.5) {
//        return YES;
//    }else{
//        return NO;
//    }
//}
/**
 *  是否支持多个手势同时处理
 *
 *  @param gestureRecognizer      。。
 *  @param otherGestureRecognizer 。。
 *
 *  @return return value bool
 */
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
//UITapGestureRecognizer(敲击)
//UIPinchGestureRecognizer(捏合，用于缩放)
//UIPanGestureRecognizer(拖拽)
//UISwipeGestureRecognizer(轻扫)
//UIRotationGestureRecognizer(旋转)
//UILongPressGestureRecognizer(长按)
@end
